package com.joymart.qualification.model;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.joymart.common.model.Range;
import com.joymart.user.model.BusinessRole;
import com.joymart.user.model.CachedUser;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.function.Predicate;


/**
 *  开店以上的评估
 */
@Data
public class AdvanceEvaluation {

    @Id
    private String id;
    private String userId;
    @Transient
    @JsonIgnore
    private CachedUser evaluateUser;
    // 评估依据区间
    private Range range;
    // 评估目标
    private BusinessRole targetRole;
    //团队业绩要求
    private BigDecimal groupPvSpec;
    private BigDecimal groupPv;
    //夹心超越，业绩要求减半
    private boolean superSupported;

    //第一结构要求
    private String primaryStructureDescription;
    @JsonIgnore
    @Transient
    private Predicate<CachedUser> primaryStructurePredicate;
    private int primaryStructureSpec;
    private int primaryStructureMatched;
    public boolean isPrimaryStructurePassed(){
        return primaryStructurePredicate == null || primaryStructureMatched >=  primaryStructureSpec;
    }



    //第二结构要求
    private String secondaryStructureDescription;
    @JsonIgnore
    @Transient
    private Predicate<CachedUser> secondaryStructurePredicate;
    private int secondaryStructureSpec;
    private int secondaryStructureMatched;
    public boolean isSecondaryStructurePassed(){
        return secondaryStructurePredicate == null || secondaryStructureMatched >=  secondaryStructureSpec;
    }

    public boolean isPvPassed() {
        return groupPvSpec == null ||  groupPv.compareTo(groupPvSpec) >= 0;
    }

    public boolean isStructurePassed() {
        return isPrimaryStructurePassed() && isSecondaryStructurePassed();
    }

    public boolean isPassed() {
        return isPvPassed() && isStructurePassed();
    }

    private LocalDateTime createdAt;

    /**
     * 累加PV计算 所采用的粒度
     * @return
     */
    public BusinessRole getCollectPvScale() {
        if(targetRole == BusinessRole.AG_AGENT){
            return BusinessRole.AU_WHS;
        }else if(targetRole == BusinessRole.AG_WHS){
            return BusinessRole.AU_SHOP;
        }else{
            return null;
        }
    }
}
